﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkSummary.Business;
using WorkSummary.Bussiness.Dal;
using WorkSummary.Bussiness.Model;
using WorkSummary.Common;
using WorkSummary.Common.Data;
using WorkSummary.Common.log;
using WorkSummary.Common.Provider;

namespace WorkSummary.Bussiness.Bll
{
    public class BUser
    {
        public static BUser Instance
        {
            get { return SingletonProvider<BUser>.Instance; }
        }

        public string GetUserName(string userid) {
            var u=DUser.Instance.Get("UserID",userid);
            return u.UserName;
        }

        public TopResult AddUser(MUser u)
        {
            try
            {
                var user = DUser.Instance.Get("AccountNum", u.AccountNum);
                if (user != null)
                {
                    return new TopResult("用户名已被使用");
                }
                u.UserID = Guid.NewGuid().ToString();
                u.CreateTime = DateTime.Now;
                u.CreateUserID = SysVisitor.Instance.UserID;
                DUser.Instance.Insert(u);
                log.info(SysVisitor.Instance.AccountNum, "新增用户", "新增用户" + u.UserID);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "新增用户", "新增用户出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }
        public TopResult ModifyUser(MUser u)
        {
            try
            {
                var user = DUser.Instance.Get("AccountNum", u.AccountNum);
                if (user != null && u.UserID != user.UserID)
                {
                    return new TopResult("用户名已被使用");
                }
                var oldUser = DUser.Instance.Get("UserID", u.UserID);
                u.CreateTime = oldUser.CreateTime;
                u.CreateUserID = oldUser.CreateUserID;
                DUser.Instance.Update("UserID", u);
                log.info(SysVisitor.Instance.AccountNum, "修改用户", "修改用户：" + u.UserID);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "修改用户", "修改用户出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public TopResult DelUser(string ids)
        {
            try
            {
                string sql = string.Format("delete from t_user where UserID in ({0})", ids);
                DbUtils.ExecuteNonQuery(sql);
                log.info(SysVisitor.Instance.AccountNum, "删除用户", "删除用户" + ids);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "删除用户", "删除用户出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public List<MUser> GetAllUser(string username)
        {
            List<MUser> list = DUser.Instance.GetAll().ToList();
            if (!string.IsNullOrEmpty(username))
            {
                list = list.Where(t => t.UserName.Contains(username)).ToList();
            }
            return list;
        }
        public List<MUser> GetAllUser()
        {
            List<MUser> list = DUser.Instance.GetAll().OrderBy(t=>t.CreateTime).ToList();
            return list;
        }

        public TopResult ChangePwd(string old_pwd, string new_pwd)
        {
            try
            {
                var user = SysVisitor.Instance.CurrentUser;
                if (user.PassWord == old_pwd)
                {
                    user.PassWord = new_pwd;
                    DUser.Instance.Update("UserID", user);
                    log.info(SysVisitor.Instance.AccountNum, "修改密码", "修改密码成功");
                    return new TopResult();
                }
                else
                {
                    return new TopResult("原密码不正确");
                }
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "修改密码", "修改密码失败", ex);
                return new TopResult("修改密码失败");
            }
        }

        public TopResult UserLogin2(string AccountNum, string password)
        {
            if (string.IsNullOrEmpty(AccountNum) || string.IsNullOrEmpty(password))
            {
                return new TopResult(-1, "用户名密码不可为空");
            }

            MUser u = null;
            u = DUser.Instance.Get("AccountNum", AccountNum);
            if (u != null && password == u.PassWord)
            {
                return new TopResult(0, u);
            }
            else
            {
                return new TopResult(-1, "用户名或密码不正确");
            }
        }

        public TopResult LoginSys(string AccountNum, string password)
        {
            var loginResult = Login(AccountNum, password);
            if (loginResult)
            {
                log.info(AccountNum, "用户登陆", "用户登陆成功");
                return new TopResult();
            }
            else
            {
                return new TopResult(-1, "用户名或密码不正确");
            }
        }

        public bool Login(string AccountNum, string password)
        {
            try
            {
                if (string.IsNullOrEmpty(AccountNum) || string.IsNullOrEmpty(password))
                {
                    return false;
                }

                MUser u = null;
                u = DUser.Instance.Get("AccountNum", AccountNum);
                if (u == null)
                {
                    return false;
                }
                //直接比较
                if (u.PassWord.Equals(password))
                {
                    SysVisitor.Instance.UserID = u.UserID;
                    SysVisitor.Instance.AccountNum = u.AccountNum;
                    SysVisitor.Instance.UserName = u.UserName;
                    SysVisitor.Instance.CurrentUser = u;

                    //写入cookie
                    CookieHelper.WriteCookie(SysVisitor.CookieNameKey, SysVisitor.CookieAccountNumKey, u.UserID, "", 1);
                    CookieHelper.WriteCookie(SysVisitor.CookieNameKey, SysVisitor.CookiePasswordKey, password, "", 1);
                    CookieHelper.WriteCookie(SysVisitor.CookieNameKey, SysVisitor.CookieUserNameKey, u.UserName, "", 1);

                    return !SysVisitor.Instance.IsGuest;
                }

                return false;
            }
            catch (Exception ex)
            {
                log.error("用户登录", "用户登录失败", ex);
                return false;
            }
        }

        public bool UserLogin()
        {
            string AccountNum = CookieHelper.GetCookie(SysVisitor.CookieNameKey, SysVisitor.CookieAccountNumKey);
            string Password = CookieHelper.GetCookie(SysVisitor.CookieNameKey, SysVisitor.CookiePasswordKey);

            if (string.IsNullOrEmpty(AccountNum) || string.IsNullOrEmpty(Password))
            {
                return false;
            }

            return Login(AccountNum, Password);
        }
        public TopResult LogOut()
        {
            SysVisitor.Instance.LoginOut();
            return new TopResult();
        }
    }
}
